//
//  UITableView+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UITableView+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation UITableView (THHook)
+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ExchangeImplementationsInTwoClasses([UITableView class],@selector(scrollToRowAtIndexPath:atScrollPosition:animated:),
                                    [UITableView class], @selector(qmui_scrollToRowAtIndexPath:atScrollPosition:animated:));
    });
}
- (void)qmui_scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
    if (!indexPath) {
        return;
    }
    BOOL isIndexPathLegal = YES;
    NSInteger numberOfSections = [self numberOfSections];
    if (indexPath.section >= numberOfSections) {
        isIndexPathLegal = NO;
    } else if (indexPath.row != NSNotFound) {
        NSInteger rows = [self numberOfRowsInSection:indexPath.section];
        isIndexPathLegal = indexPath.row < rows;
    }
    if (!isIndexPathLegal) {
        NSLog(@"UITableView (QMUI)%@ - target indexPath : %@ ，不合法的indexPath。\n%@", self, indexPath, [NSThread callStackSymbols]);
        NSString *errorDes = [NSString stringWithFormat:@"TableView (QMUI)%@ - target indexPath : %@ ，不合法的indexPath。\n%@",self, indexPath, [NSThread callStackSymbols]];
        NSError *error = [NSError errorWithDomain:@"huanque" code:10000 userInfo:@{NSLocalizedDescriptionKey:errorDes}];
        [[THCrashManager shareInstance] reportError:error];
    } else {
        [self qmui_scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
    }
}

@end
